<template>
  <div>
    <!-- 面包屑 -->
    <cru msg1="权限" msg2="角色"></cru>
    <!-- 分配权限 -->
    <el-dialog title="角色分配" :visible.sync="distributeDialog" width="50%">
      <el-form ref="distributeFormRef" :model="distributeForm" label-width="80px">
        <el-form-item label="角色名称">{{distributeForm.roleName}}</el-form-item>
        <el-form-item label="角色权限">
          <el-tree
            :data="distributeTree"
            :props="defaultProps"
            show-checkbox
            default-expand-all
            :default-checked-keys="xzid"
            node-key="id"
            ref="tree"
          ></el-tree>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="distributeDialog = false">取 消</el-button>
        <el-button type="primary" @click="getChecked()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-button type="primary" round>添加角色</el-button>
      <el-table :data="distributeData" style="width: 100%" :stripe="true" :border="true">
        <el-table-column type="expand">
          <template slot-scope="info">
            <el-row
              v-for="(v,k) in info.row.son"
              :key="v.id"
              :style="{'border-bottom':'1px solid rgb(235, 238, 245)',
              'border-top':k==0?'1px solid rgb(235, 238, 245)':''}"
            >
              <el-col :span="5">
                <el-tag closable @close="handleClose(info.row,v.id)">{{v.authName}}</el-tag>
              </el-col>
              <el-col :span="19">
                <el-row
                  v-for="(vv,k) in v.children"
                  :key="vv.id"
                  :style="{'border-top':k==0?'':'1px solid rgb(235, 238, 245)'}"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      @close="handleClose(info.row,vv.id)"
                      type="success"
                    >{{vv.authName}}</el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="vvv in vv.children"
                      :key="vvv.id"
                      closable
                      @close="handleClose(info.row,vvv.id)"
                    >{{vvv.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="60"></el-table-column>
        <el-table-column prop="roleName" label="角色名称" width="160"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="info">
            <el-tooltip content="编辑" placement="top" :enterable="false">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="edit(info.row.id)"
              >编辑</el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top" :enterable="false">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="del(info.row.id)"
              >删除</el-button>
            </el-tooltip>
            <el-tooltip content="分配权限" placement="top" :enterable="false">
              <el-button
                type="warning"
                icon="el-icon-setting"
                size="mini"
                @click="fenpei(info.row)"
              >分配权限</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.getData()
  },
  data() {
    return {
      // 选中的id
      xzid: [],
      // 权限树列表
      distributeTree: [],
      // 权限分配列表
      distributeData: [],
      // 弹出表单
      distributeDialog: false,
      // model小对象
      distributeForm: {},
      defaultProps: {
        children: 'children',
        label: 'authName'
      }
    }
  },
  methods: {
    //   获取列表
    async getData() {
      const { data: dt } = await this.$http.get('roles')
      if (dt.meta.status !== 200) {
        return this.$message.error(dt.meta.msg)
      }

      dt.data.forEach(item => {
        item.son = item.children
        delete item.children
      })
      console.log(dt)
      this.distributeData = dt.data
    },
    // 删除
    handleClose(row, vid) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const { data: dt } = await this.$http.delete(
            `roles/${row.id}/rights/${vid}`
          )
          // console.log(dt)
          if (dt.meta.status !== 200) {
            return this.$message.error(dt.meta.msg)
          }
          this.$message.success(dt.meta.msg)
          row.son = dt.data
        })
        .catch(() => {})
    },
    // 分配权限
    async fenpei(role) {
      this.distributeForm = role
      this.distributeDialog = true
      const { data: dt } = await this.$http.get('rights/tree')
      // console.log(dt);
      this.distributeTree = dt.data
      // 获取选中的id
      var keyId = []
      this.getId(role, keyId)
      this.xzid = keyId
      // console.log(keyId)
    },
    getId(data, arr) {
      if (data.son) {
        data.children = data.son
      }
      if (!data.children) {
        return arr.push(data.id)
      }
      data.children.forEach(item => {
        this.getId(item, arr)
      })
    },
    // 提交选择的权限给数据库
    async getChecked() {
      const str1 = this.$refs.tree.getHalfCheckedKeys()
      const str2 = this.$refs.tree.getCheckedKeys()
      const str = str1.concat(str2).join(',')
      console.log(str)
      const { data: dt } = await this.$http.post(
        `roles/${this.distributeForm.id}/rights`,
        { rids: str }
      )
      if (dt.meta.status !== 200) {
        return this.$message.error(dt.meta.msg)
      }
      this.$message.success(dt.meta.msg)
      this.distributeDialog = false
      this.getData()
    }
  }
}
</script>

<style lang="less" scoped>
.el-row {
  display: flex;
  align-items: center;
}
.el-tag {
  margin: 10px 5px;
}
</style>
